package com.stormrage.edu.dao.ent.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.ent.EntAcctDao;
import com.stormrage.entity.ent.EntAcct;
import com.stormrage.entity.ent.rep.EntAcctRep;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Repository
public class EntAcctDaoImpl extends MysBaseDao implements EntAcctDao {

	private static final Logger logger = LoggerFactory.getLogger(EntAcctDaoImpl.class);

	@Override
	public void updateLock(final EntAcct arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 Update 】-【 STARTED 】");

		final String query = "UPDATE ent_acct SET locked = :locked WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 Update 】-【 COMPLETED 】");

	}

	@Override
	public void updatePass(EntAcct arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 UpdatePass 】-【 STARTED 】");

		final String query = "UPDATE ent_acct SET pass = :pass, salt = :salt WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 UpdatePass 】-【 COMPLETED 】");

	}

	@Override
	public EntAcct queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryById 】-【 STARTED 】");

		final String query = "SELECT * FROM ent_acct WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		EntAcct entity = queryObject(query, EntAcct.class, map);
		logger.debug(" 查询结果为 " + entity);

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public EntAcct queryByIdLock(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryByIdLock 】-【 STARTED 】");

		final String query = "SELECT * FROM ent_acct WHERE id = :id AND locked = :locked";
		logger.debug(" 数据库执行语句为 " + query);

		EntAcct entity = queryObject(query, EntAcct.class, map);
		logger.debug(" 查询结果为 " + entity);

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryByIdLock 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public List<EntAcct> queryListByEntIdLock(Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryListByEntIdLock 】-【 STARTED 】");

		final String query = "SELECT * FROM ent_acct WHERE ent_id = :entId AND locked = :locked";
		logger.debug(" 数据库执行语句为 " + query);

		List<EntAcct> array = queryList(query, EntAcct.class, map);
		logger.debug(" 查询结果为 " + array);

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryListByEntIdLock 】-【 COMPLETED 】");
		return array;

	}

	@Override
	public List<EntAcctRep> queryListForPass() throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryListForPass 】-【 STARTED 】");

		final String query = "SELECT a.id, a.acct, a.real_name, a.ent_id, b.enterprise"
				+ " FROM ent_acct AS a, ent_enterprise AS b WHERE a.ent_id = b.id AND locked = 1";
		logger.debug(" 数据库执行语句为 " + query);

		List<EntAcctRep> array = queryList(query, EntAcctRep.class);
		logger.debug(" 查询结果为 " + array);

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryListForPass 】-【 COMPLETED 】");
		return array;

	}

}
